package com.rainng.coursesystem.controller.admin;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rainng.coursesystem.base.api.dto.BaseIdReqDTO;
import com.rainng.coursesystem.base.api.dto.BaseIdsReqDTO;
import com.rainng.coursesystem.base.api.dto.PagingReqDTO;
import com.rainng.coursesystem.base.utils.excel.ExportExcel;
import com.rainng.coursesystem.controller.BaseController;
import com.rainng.coursesystem.model.excelvo.TaskExcelVo;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.module.task.dto.request.GroupTaskExportReqDTO;
import com.rainng.coursesystem.module.task.dto.request.TaskDetailDTO;
import com.rainng.coursesystem.module.task.dto.request.TaskExportReqDTO;
import com.rainng.coursesystem.module.task.dto.request.TaskQueryReqDTO;
import com.rainng.coursesystem.module.task.dto.response.TaskPagingResDTO;
import com.rainng.coursesystem.module.task.dto.response.CodeWhiteListResDTO;
import com.rainng.coursesystem.module.task.dto.request.TaskCarDTO;
import com.rainng.coursesystem.module.task.dto.response.TaskCarResDTO;
import com.rainng.coursesystem.module.task.dto.request.TaskConfigDTO;
import com.rainng.coursesystem.module.task.service.TaskService;
import com.rainng.coursesystem.util.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import com.rainng.coursesystem.model.entity.CodeWhiteEntity;
import com.rainng.coursesystem.module.task.dto.request.CodeWhiteListDTO;
import com.rainng.coursesystem.module.task.dto.request.StRuleListDTO;


//@Admin(Admin.DEPARTMENT_MANAGE)
@RestController
@Slf4j
@RequestMapping("/v2/sys/task")
@Api(tags = "任务管理")
public class TaskManagerController extends BaseController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private ExportExcel exportExcel;

    @PostMapping("/paging")
    @ApiOperation(value = "任务管理数据分页查询")
    public ResultVO<IPage<TaskPagingResDTO>> paging(@RequestBody PagingReqDTO<TaskQueryReqDTO> reqDTO) {
        IPage<TaskPagingResDTO> res = taskService.paging(reqDTO);
        List<TaskPagingResDTO> list = res.getRecords();
        Integer n = 1;
        for(TaskPagingResDTO t :list){
            t.setOrder(n.toString());
            n ++;
        }
        return super.result(res);
    }
    /*子任务nid为空查询*/
    @PostMapping("/taskChangePaging")
    @ApiOperation(value = "子任务nid为空数据分页查询")
    public ResultVO<IPage<TaskPagingResDTO>> taskChangePaging(@RequestBody PagingReqDTO<TaskQueryReqDTO> reqDTO) {
        IPage<TaskPagingResDTO> res = taskService.taskChangePaging(reqDTO);
        List<TaskPagingResDTO> list = res.getRecords();
        Integer n = 1;
        for(TaskPagingResDTO t :list){
            t.setOrder(n.toString());
            n ++;
        }
        return super.result(res);
    }

    /**
     * 根据传入id,导出任务列表数据
     *
     * @param response
     * @param reqDTO
     * @return
     */
    @PostMapping("/export")
    @ApiOperation(value = "任务管理数据导出")
    public ResultVO export(HttpServletResponse response, @RequestBody BaseIdsReqDTO reqDTO) {
        List<TaskExportReqDTO> res = taskService.getTaskListByIds(reqDTO);
        exportExcel.export(response, TaskExportReqDTO.class, "任务列表", res);
        return super.result();
    }

    /**
     * 根据传入id,导出任务列表数据
     *
     * @param response
     * @return
     */
    @PostMapping("/exportAllTask")
    @ApiOperation(value = "任务管理数据全部导出")
    public ResultVO export(HttpServletResponse response) {
        List<TaskExportReqDTO> res = taskService.getAllTask();
        exportExcel.export(response, TaskExportReqDTO.class, "任务列表", res);
        return super.result();
    }

//    @GetMapping("/exportAllTask")
//    @ApiOperation(value = "任务管理数据全部导出")
//    public ResultVO export22(HttpServletResponse response){
//        List<TaskExportReqDTO> res = taskService.getAllTask();
//        exportExcel.export(response, TaskExportReqDTO.class,"任务列表",res);
//        return super.result();
//    }

    @GetMapping("/export/admin")
    @ApiOperation(value = "全局报表")
    public ResultVO exportAll(HttpServletResponse response) {
        List<GroupTaskExportReqDTO> res = taskService.getTaskByAdmin();
        exportExcel.export(response, GroupTaskExportReqDTO.class, "全局统计报表", res);
        return super.result();
    }

    @PostMapping("/save")
    @ApiOperation(value = "任务管理数据新增和更新")
    public ResultVO save(@RequestBody TaskDetailDTO reqDto) {
        return taskService.save(reqDto);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "任务管理数据详情")
    public ResultVO<TaskPagingResDTO> detail(@RequestBody BaseIdReqDTO reqDTO) {
        TaskPagingResDTO res = taskService.detail(reqDTO.getId());
        return super.result(res);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "任务管理数据删除")
    public ResultVO delete(@RequestBody BaseIdsReqDTO reqDTO) {
        //
        List<String> ids = reqDTO.getIds();
        if(ids.size() > 0){
            int k = taskService.deleteTaskChangeByFatherIds(ids);
            System.out.println("产品变更删除接口调用成功");
        }
        taskService.removeByIds(reqDTO.getIds());
        return super.result();
    }
    @PostMapping("/setTaskImportant")
    @ApiOperation(value = "任务管理数据标记重要")
    public ResultVO setTaskImportant(@RequestBody TaskDetailDTO reqDto) {
        return taskService.setTaskImportant(reqDto);
    }

    @PostMapping("/saveCodeWhiteList")
    @ApiOperation(value = "白名单参数保存")
    public ResultVO saveCodeWhiteList(@RequestBody CodeWhiteEntity reqDto) {
        return taskService.saveCodeWhiteList(reqDto);
    }
    @PostMapping("/updateCodeWhiteList")
    @ApiOperation(value = "白名单参数更新")
    public ResultVO updateCodeWhiteList(@RequestBody CodeWhiteEntity reqDto) {
        return taskService.updateCodeWhiteList(reqDto);
    }
    @PostMapping("/deleteCodeWhiteList")
    @ApiOperation(value = "白名单参数删除")
    public ResultVO deleteCodeWhiteList(@RequestBody CodeWhiteEntity reqDto) {
        return taskService.deleteCodeWhiteList(reqDto);
    }
    @PostMapping("/getCodeWhiteList")
    @ApiOperation(value = "白名单参数查询")
    public ResultVO getCodeWhiteList(@RequestBody PagingReqDTO<CodeWhiteListDTO> reqDTO) {
        IPage<CodeWhiteListResDTO> res = taskService.getCodeWhiteList(reqDTO);
        List<CodeWhiteListResDTO> list = res.getRecords();
        Integer n = 1;
        for(CodeWhiteListResDTO t :list){
            // t.setOrder(n.toString());
            n ++;
        }
        return super.result(res);
    }
    @PostMapping("/saveTaskConfig")
    @ApiOperation(value = "保存配置")
    public ResultVO saveTaskConfig(@RequestBody TaskConfigDTO reqDto) {
        return taskService.saveTaskConfig(reqDto);
    }
    @PostMapping("/updateTaskConfig")
    @ApiOperation(value = "更新配置")
    public ResultVO updateTaskConfig(@RequestBody TaskConfigDTO reqDto) {
        return taskService.updateTaskConfig(reqDto);
    }
    @PostMapping("/getTaskConfig")
    @ApiOperation(value = "获取配置信息")
    public ResultVO getTaskConfig(@RequestBody TaskConfigDTO reqDto) {
        TaskConfigDTO res = taskService.getTaskConfig(reqDto.getTaskCode());
        return super.result(res);
    }
    @PostMapping("/saveTaskCar")
    @ApiOperation(value = "绑定车辆vin及其余参数")
    public ResultVO saveTaskCar(@RequestBody TaskCarDTO reqDto) {
        return taskService.saveTaskCar(reqDto);
    }

    @PostMapping("/updateTaskCar")
    @ApiOperation(value = "更新车辆vin及其余参数")
    public ResultVO updateTaskCar(@RequestBody TaskCarDTO reqDto) {
        return taskService.updateTaskCar(reqDto);
    }
    @PostMapping("/checkCar")
    @ApiOperation(value = "查询车辆vin是否存在")
    public ResultVO checkCar(@RequestBody TaskCarDTO reqDto) {
        List<TaskCarResDTO> carList = taskService.getCarListByVIN(reqDto.getVin(), reqDto.getTaskCode());
        if (carList.size() > 0) {
            return super.result(0);
        } else {
            return super.result(1);
        }
    }
    @PostMapping("/getTaskCarByApplyNum")
    @ApiOperation(value = "获取车辆参数列表")
    public ResultVO<TaskCarResDTO>getTaskCarByApplyNum(@RequestBody TaskCarDTO reqDto) {
        List<TaskCarResDTO> res = taskService.getTaskCarByTaskCode(reqDto.getTaskCode());
        return super.result(res);
    }
    @PostMapping("/getTaskCarDetail")
    @ApiOperation(value = "获取车辆参数详情")
    public ResultVO<TaskCarResDTO>getTaskCarDetail(@RequestBody TaskCarDTO reqDto) {
        TaskCarResDTO res = taskService.getTaskCarDetail(reqDto.getConfigIndex(), reqDto.getTaskCode());
        return super.result(res);
    }

    @PostMapping("/getCarList")
    @ApiOperation(value = "获取样车列表")
    public ResultVO<TaskCarResDTO>getCarList(@RequestBody TaskCarDTO reqDto) {
        List<TaskCarResDTO> res = taskService.getCarList(reqDto.getTaskCode());
        return super.result(res);
    }

    @PostMapping("/getStRuleList")
    @ApiOperation(value = "视同规则列表")
    public ResultVO getStRuleList(@RequestBody PagingReqDTO<StRuleListDTO> reqDTO) {
        IPage<StRuleListDTO> res = taskService.getStRuleList(reqDTO);
        List<StRuleListDTO> list = res.getRecords();
        Integer n = 1;
        for(StRuleListDTO t :list){
            // t.setOrder(n.toString());
            n ++;
        }
        return super.result(res);
    }

    @PostMapping("/saveStRuleList")
    @ApiOperation(value = "新增视同规则")
    public ResultVO saveStRuleList(@RequestBody StRuleListDTO reqDTO) {
        return taskService.saveStRuleList(reqDTO);
    }
    @PostMapping("/updateStRule")
    @ApiOperation(value = "视同规则修改")
    public ResultVO updateStRule(@RequestBody StRuleListDTO reqDto) {
        return taskService.updateStRule(reqDto);
    }
    @PostMapping("/deleteStRule")
    @ApiOperation(value = "视同规则删除")
    public ResultVO deleteStRule(@RequestBody StRuleListDTO reqDTO) {
        return taskService.deleteStRule(reqDTO.getId());
    }
    @GetMapping("/excel/module/download")
    @ApiOperation(value = "7.26任务导入模板")
    public void downloadManagerModule(HttpServletResponse response) throws IOException {
        EasyExcelUtil.downloadTitle(response, TaskExcelVo.class, "任务导入模板");
        //return super.result("导入成功,请稍后查看");
    }

    @PostMapping("/excel/upload")
    @ApiOperation(value = "7.26任务导入上传接口")
    public ResultVO importExcelSubject(MultipartFile file) throws IOException {

        log.info("入参");
        List<TaskExcelVo> list = EasyExcelUtil.read(file.getInputStream(),
                TaskExcelVo.class);

        if (list.size() > 1000) {
            return super.failedResult("单次导入不能超过1000条");
        }
        CollectionUtil.reverse(list);
        taskService.saveTasks(list);
        return super.result("导入成功,请稍后查看");
    }

//    @GetMapping("/{id}")
//    public ResultVO get(@PathVariable Integer id) {
//        return service.get(id);
//    }
//    @PostMapping
//    public ResultVO create(@RequestBody @Validated DepartmentEntity entity) {
//        return service.create(entity);
//    }
//
//    @DeleteMapping("/{id}")
//    public ResultVO delete(@PathVariable Integer id) {
//        return service.delete(id);
//    }
//
//    @PutMapping
//    public ResultVO update(@RequestBody @Validated DepartmentEntity entity) {
//        return service.update(entity);
//    }
//
//    @RequestMapping("/page/count")
//    public ResultVO getPageCount(String name) {
//        return service.getPageCount(name);
//    }
//
//    @RequestMapping("/page")
//    public ResultVO getPage(String name) {
//        return service.getPage(1, name);
//    }
//
//    @RequestMapping("/page/{index}")
//    public ResultVO getPage(@PathVariable Integer index, String name) {
//        return service.getPage(index, name);
//    }
//
//    @Admin
//    @RequestMapping("/names")
//    public ResultVO listName() {
//        return service.listName();
//    }
}
